<template>
    <div class="container-content">
        <div class="container-content-title">{{ $t('platform.setting.config.title') }}
            <el-button type="text" class="p-0" @click="dialogUpdate" v-if="dataStatus == 'dle'">{{ $t('action.edit') }}</el-button>
        </div>
        <component-page-loading :status="dataStatus" v-if="['loading', 'error'].indexOf(dataStatus) >= 0" @reload="getData"></component-page-loading>
        <el-form label-width="180px" class="detail" v-else>
            <el-row>
                <el-col :xl="12">
                    <el-form-item :label="$t('platform.setting.config.payment')">
                        <el-tag effect="plain" size="mini" type="info" v-if="data.payment != null">
                            {{ $t('platform.setting.config.paymentList')[data.payment].label }}
                        </el-tag>
                        <component-page-loading status="nodata" v-else></component-page-loading>
                    </el-form-item>
                </el-col>
                <el-col :xl="12">
                    <el-form-item :label="$t('platform.setting.config.rate')">
                        <el-tag effect="plain" size="mini" type="info" v-if="data.rate != null">
                            <component-page-numeral :num="data.rate" format="0.[00]%"></component-page-numeral>
                        </el-tag>
                        <component-page-loading status="nodata" v-else></component-page-loading>
                    </el-form-item>
                </el-col>
                <el-col :xl="24">
                    <el-form-item :label="$t('platform.setting.config.currency')">
                        <el-tag v-for="(code,index) in data.currency" :key="index" effect="plain" size="mini" type="info" class="mr-1">{{ code }}</el-tag>
                        <component-page-loading status="nodata" v-if="!data.currency.length"></component-page-loading>
                    </el-form-item>
                </el-col>
            </el-row>
        </el-form>
        <el-dialog :title="$t('platform.setting.config.dialog.update')" width="480px" :visible.sync="dialog.visible.update">
            <div class="pr-12" style="min-height: 330px">
                <el-form :model="dataCache" label-width="80px">
                    <el-form-item :label="$t('platform.setting.config.currency')" :error="Boolean(msg.errors['currency']) ? msg.errors['currency'][0] : ''" required>
                        <component-page-option-currency v-model="dataCache.currency" class="block"></component-page-option-currency>
                    </el-form-item>
                    <el-form-item :label="$t('platform.setting.config.payment')" :error="Boolean(msg.errors['payment']) ? msg.errors['payment'][0] : ''" required>
                        <el-select class="block" v-model="dataCache.payment" :disabled="dataCache.id != null">
                            <el-option v-for="item in $t('platform.setting.config.paymentList')" :key="item.value" :label="item.label" :value="item.value" :disabled="item.value == 2"></el-option>
                        </el-select>
                        <el-alert type="warning" :closable="false" class="leading-normal mt-3 px-1"
                                  v-if="$te(`platform.setting.config.paymentList.${dataCache.payment}`)"
                                  :title="$t(`platform.setting.config.paymentList.${dataCache.payment}.desc`)">
                        </el-alert>
                    </el-form-item>
                    <el-form-item :label="$t('platform.setting.config.rate')" :error="Boolean(msg.errors['rate']) ? msg.errors['rate'][0] : ''">
                        <el-col :span="11">
                            <el-input type="number" v-model.number="dataCache.rate" min="0" max="1" step="0.01"></el-input>
                        </el-col>
                        <el-col :span="2" class="text-center">
                            <i class="el-icon-right"></i>
                        </el-col>
                        <el-col :span="11">
                            <el-input :value="!Boolean(dataCache.rate) ? null : $store.getters['config/filterNumeral'](dataCache.rate * 100)" disabled>
                                <span slot="suffix">%</span>
                            </el-input>
                        </el-col>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" @click="onSubmit">{{ $t('action.confirm') }}</el-button>
                        <el-button @click="dialog.visible.update = false">{{ $t('action.cancel') }}</el-button>
                    </el-form-item>
                </el-form>
            </div>
        </el-dialog>
    </div>
</template>

<script>
export default {
    name: "PlatformConfig",
    props: ['tree'],
    data() {
        return {
            dataStatus: 'dle',
            data: {
                plat_code: null,
                currency: [],
                payment: null,
                rate: null,
            },
            dataCache: {},
            dataInit: {},
            dialog: {
                visible: {
                    update: false,
                }
            },
            msg: {
                code: 200,
                message: '',
                errors: {},
            },
        }
    },
    watch: {
        tree: {
            deep: true,
            immediate: true,
            handler(n, o) {
                // 获取数据
                n.id != null && this.getData();
            }
        },
    },
    created() {
        // 备份初始数据
        this.dataInit = _.cloneDeep(this.data);
    },
    methods: {
        // 获取数据
        getData() {
            this.dataStatus = 'loading';
            axios.get('/platform/setting/config/getData', {
                params: {
                    plat_code: this.tree.code
                }
            }).then((response) => {
                if (_.includes([200, 44100], response.data.resp_msg.code)) {
                    this.data = response.data.resp_data || _.cloneDeep(this.dataInit);
                    this.dataStatus = 'dle';
                    // 广播事件到父组件
                    this.$emit('update', this.data);
                } else {
                    this.dataStatus = 'error';
                }
            })
        },
        initMsg() {
            this.msg.code = 200;
            this.msg.message = '';
            this.msg.errors = {};
        },
        // 显示模态框（编辑）
        dialogUpdate() {
            this.initMsg();
            // 缓存数据
            this.dataCache = _.cloneDeep(this.data);
            // 平台code
            this.dataCache.plat_code = this.tree.code;
            // 平台税率
            if (Boolean(this.dataCache.rate)) {
                this.dataCache.rate = this.$store.getters['config/filterNumeral'](this.dataCache.rate);
            }
            // 显示模态框
            this.dialog.visible.update = true;
        },
        onSubmit() {
            this.initMsg();
            // loading 状态 start
            let loading = this.$loading();
            // 保存数据
            axios.post('/platform/setting/config/updateOrCreate', this.dataCache).then((response) => {
                // loading 状态 close
                loading.close();
                // 逻辑处理
                if (response.data.resp_msg.code == 200) {
                    this.$message({
                        type: 'success',
                        message: this.$t('messages.succeeded', {status: this.$t('action.edit')}),
                        showClose: true
                    });
                    // 更新数据
                    this.data = response.data.resp_data;
                    // 关闭模态框
                    this.dialog.visible.update = false;
                    // 广播事件到父组件
                    this.$emit('update', this.data);
                } else if (response.data.resp_msg.code == 42000) {
                    this.$message({
                        type: 'error',
                        message: response.data.resp_msg.message,
                        showClose: true
                    });
                    this.msg = response.data.resp_msg;
                } else {
                    this.$message({
                        type: 'error',
                        dangerouslyUseHTMLString: true,
                        message: this.$t('messages.failed', {status: this.$t('action.edit')}) + `<div class="mt-2">${response.data.resp_msg.message}</div>`,
                        showClose: true
                    });
                }
            })
        },
    }
}
</script>

<style scoped>

</style>
